﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Asah.SiteWorx.Commerce;

public partial class SiteWorx_PrintOrder : System.Web.UI.Page
{
    private int SelectedOrderId
    {
        get
        {
            return Convert.ToInt32(Session["SelectedOrderId"].ToString());
        }
        set
        {
            Session["SelectedOrderId"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadOrderDetails();
        }
    }

    private void LoadOrderDetails()
    {

        OrderInfo orderInfo = OrderList.GetOrderInfo(SelectedOrderId);

        if (orderInfo != null)
        {
            lblOrderNumber.Text = orderInfo.ID.ToString();
            lblOrderStatus.Text = orderInfo.Status.ToString();
            lblOrderDate.Text = orderInfo.CreatedDate.ToString();

            //Build buyer information string.
            ProfileCommon profileCommon;
            profileCommon = Profile.GetProfile(orderInfo.CreatedBy.Name);
            System.Text.StringBuilder buyerDetails = new System.Text.StringBuilder();

            buyerDetails.Append(profileCommon.Company + "<br>");
            buyerDetails.Append(profileCommon.FirstName + " " + profileCommon.LastName + "<br>");
            buyerDetails.Append(profileCommon.StreetAddress + "<br>");
            buyerDetails.Append(profileCommon.City + " " + profileCommon.State + ", " + profileCommon.Zip + "<br>");
            buyerDetails.Append(profileCommon.Phone + "<br>");
            buyerDetails.Append("<a href=\"mailto:" + orderInfo.CreatedBy.Email + "\">" + orderInfo.CreatedBy.Email + "</a><br>");
            buyerDetails.Append("<br>");
            buyerDetails.Append("Tax ID:&nbsp;" + profileCommon.TaxId);
            lblBuyerDetails.Text = buyerDetails.ToString();

            //Build shipping information string.
            System.Text.StringBuilder shippingDetails = new System.Text.StringBuilder();

            shippingDetails.Append(orderInfo.ShipToInfo.Company + "<br>");
            shippingDetails.Append(orderInfo.ShipToInfo.FirstName + " " + orderInfo.ShipToInfo.LastName + "<br>");
            shippingDetails.Append(orderInfo.ShipToInfo.Address1 + "<br>");
            shippingDetails.Append(orderInfo.ShipToInfo.Address2 + "<br>");
            shippingDetails.Append(orderInfo.ShipToInfo.City + " " + orderInfo.ShipToInfo.State + ", " + orderInfo.ShipToInfo.Zip + "<br>");
            shippingDetails.Append(orderInfo.ShipToInfo.Phone + "<br>");
            shippingDetails.Append("<a href=\"mailto:" + orderInfo.ShipToInfo.Email + "\">" + orderInfo.ShipToInfo.Email + "</a><br>");

            lblShippingAddressInfo.Text = shippingDetails.ToString();
            lblShipmentMethod.Text = orderInfo.ShipToInfo.ShippingMethod.ToString();

            //Build shipping information string.
            System.Text.StringBuilder billingDetails = new System.Text.StringBuilder();

            billingDetails.Append(orderInfo.BillToInfo.Company + "<br>");
            billingDetails.Append(orderInfo.BillToInfo.FirstName + " " + orderInfo.BillToInfo.LastName + "<br>");
            billingDetails.Append(orderInfo.BillToInfo.Address1 + "<br>");
            billingDetails.Append(orderInfo.BillToInfo.Address2 + "<br>");
            billingDetails.Append(orderInfo.BillToInfo.City + " " + orderInfo.BillToInfo.State + ", " + orderInfo.BillToInfo.Zip + "<br>");
            billingDetails.Append(orderInfo.BillToInfo.Phone + "<br>");
            billingDetails.Append("<a href=\"mailto:" + orderInfo.BillToInfo.Email + "\">" + orderInfo.BillToInfo.Email + "</a><br>");
            
            lblBillingAddressInfo.Text = billingDetails.ToString();

            lblCardType.Text = orderInfo.PaymentDetailInfo.PaymentMethod.ToString();
            lblNameOnCard.Text = orderInfo.PaymentDetailInfo.NameOnCard;
            lblCardNumber.Text = orderInfo.PaymentDetailInfo.GetCardNumber();
            lblExpirationMonth.Text = orderInfo.PaymentDetailInfo.ExpirationMonth;
            lblExpirationYear.Text = orderInfo.PaymentDetailInfo.ExpirationYear;

            rgvOrderItems.DataSource = orderInfo.OrderItemList;
            rgvOrderItems.DataBind();

            
        }
        else  // the order wasn't found.
        {
            Response.Redirect("OrderManagement.aspx", true);
        }
    }

    protected void rgvOrderItems_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item.ItemType == Telerik.Web.UI.GridItemType.Footer)
        {
            OrderInfo orderInfo = OrderList.GetOrderInfo(SelectedOrderId);

            Label lblGrandTotal = (Label)e.Item.FindControl("lblGrandTotal");
            lblGrandTotal.Text = String.Format("{0:c}", orderInfo.Total);
        }
    }
}
